<?php
namespace app\index\controller;
use \think\Controller;
use app\index\model\Categroy;
use app\index\model\Shop;
use app\index\model\Carousel;

class Index extends Controller
{
    public function index(Categroy $categroy,Shop $shop,Carousel $carousel)
    {
        //获取全部分类
        $list = $categroy->where('pid = 0')->select()->toArray();
        foreach ($list as $key => $values) {
            $min = $categroy->where('pid = '.$values['id'] )->select()->toArray();
            $list[$key]['min'] = $min;
            foreach ($min as $k => $value) {
                $three = $categroy->where('pid = '. $value['id'])->select()->toArray();
                $list[$key]['min'][$k]['three'] = $three;
            }
        }
        //首页轮播图
        $carousels = $carousel->select();
        //获取秒杀商品
        $spikes = $shop->where('spike',1)->where('shop_delete',0)->where('audit',1)->where('is_use',0)->field('id,image_path,shop_name,shop_start_money,shop_end_money')->order('create_time desc')->limit(4)->select();
        //服装
        $cateFirst_id = $categroy->where('categroy_name','精品女装')->where('pid',0)->field('id')->find();
        $cateSecond_id = $categroy->where('pid',$cateFirst_id['id'])->field('id')->select()->toArray();
        $result = [];
        array_walk_recursive($cateSecond_id, function($value) use (&$result) {
           array_push($result, $value);
        });
        $shops = $categroy->with('shops')->whereIn('pid', $result)->select();
        //推荐商品
        $is_news = $shop->where('shop_delete',0)->where('is_use',0)->where('audit',1)->where('is_new',1)->field('image_path,shop_name,shop_start_money,shop_end_money')->order('create_time desc')->limit(4)->select();

        return view('index/index',compact('list','carousels','spikes','is_news','shops'));
    }

    public function shopDetail()
    {
        return view('index/shopDetail');
    }

    //空方法
    public function _empty()
    {
        $this->redirect('index/index');
    }

}
